2. Running a battery of tests
You should be able to run
your tests with the click of a mouse or by entering a single command. To
run these tests, right-click the test project in Solution Explorer and
choose Debug => Start New Instance. The unit test project executes and displays the results in the Test Results menu, as shown in Figure 5.
Based on the results in Figure 5, it looks like the unit tests aren't complete enough to adequately test the code. In this case, both unit tests failed because SayHello()CreateMessage() throw System.ArgumentException if the parameter "name"
is empty. In this case, you have to tell the unit test framework and that
you're expecting these methods to throw exceptions if the parameter "name" is null or empty. To do so, you add an ExpectedException attribute to each of the test methods:
[ExpectedException(typeof(System.ArgumentException))]
public void SayHelloTest()...
Adding an ExpectedException tells the unit test framework that you're expecting your methods to throw an exception of type System.ArgumentException or System.ArgumentNullException because it's derived from System.ArgumentException. Now, when you pass string.Empty
or null to these methods, the unit tests won't fail because you're
expecting these methods to throw exceptions when these values are
provided for the parameter "name". So
to adequately test these methods, you need to provide an empty string, a
null string, and a valid string to each of these methods. You also want
to test the negative case where the expected value isn't equal to the
actual value. Listing 3 shows the complete unit test for CreateMessage.
Listing 3.
/// <summary> ///A test for CreateMessage ///</summary> [TestMethod()] [DeploymentItem("WindowsFormsApplication1.exe")] [ExpectedException(typeof(System.ArgumentException))] public void CreateMessageTest() { Form1_Accessor target = new Form1_Accessor(); string name = "Andrew"; string expected = "Hello Andrew!"; string actual; actual = target.CreateMessage(name); Assert.AreEqual(expected, actual);
name = string.Empty; expected = string.Empty;
actual = target.CreateMessage(name); Assert.AreEqual(expected, actual);
name = null; expected = null; actual = target.CreateMessage(name);
Assert.AreEqual(expected, actual);
name = "Andrew"; expected = "Hello Joe!";
Assert.AreNotEqual(expected, actual); }
|
The first test sets the parameter "name" to "Andrew". In this test, you expect the resulting message to be "Hello Andrew!" as indicated by the Assert.AreEqual(). The second test passes an empty string as the "name" parameter. Because you provided the ExpectedException attribute for the System.ArgumentException, this tests two passes. The third test passes null as the "name" parameter. This time, too, the results are what you expect because CreateMessage() throws a System.ArgumentNullException. The final test tests the negative case where "Andrew" is again passed to CreateMessage(), and you test to ensure that the result isn't equal to something other than "Hello Andrew!". In this case, "Hello Joe!" doesn't match, and the Assert.AreNotEqual is true. Now, in Figure 6, Visual Studio shows that your tests have succeeded.